package com.zhoug.camera3demo;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;

import com.zhoug.camera3.ui.CaptureImageActivity;
import com.zhoug.camera3.ui.CaptureVideoActivity;
import com.zhoug.camera3.ui.ocr.CameraOcrImageActivity;
import com.zhoug.camera3.ui.ocr.OcrType;
import com.zhoug.common3.utils.FileUtils;
import com.zhoug.common3.utils.UriUtils;
import com.zhoug.logger3.Logger;
import com.zhoug.permission3.ZPermissions;

import java.io.File;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

public class MainActivity extends MyBaseActivity {
    private static final String TAG = ">>>MainActivity";
    private ActivityResultLauncher<Intent> mLauncher1;
    private ActivityResultLauncher<Intent> mLauncher2;
    private ActivityResultLauncher<Intent> mLauncher3;
    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        ZPermissions.with(this)
                .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                .setCallback((success, granted, denied) -> {
                }).request();

        mLauncher1 = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result!=null && result.getResultCode()==RESULT_OK && result.getData()!=null){
                    Intent data = result.getData();
                    Uri uri = data.getData();
                    String path = UriUtils.getPathFromUri(getApplication(), uri);
                    Logger.d(TAG, "path1:"+path);
                }
            }
        });

        mLauncher2 = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result!=null && result.getResultCode()==RESULT_OK && result.getData()!=null){
                    Intent data = result.getData();
                    Uri uri = data.getData();
                    String path = UriUtils.getPathFromUri(getApplication(), uri);
                    Logger.d(TAG, "path2:"+path);
                }
            }
        });

        mLauncher3 = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result!=null && result.getResultCode()==RESULT_OK && result.getData()!=null){
                    Intent data = result.getData();
                    Uri uri = data.getData();
                    String path = UriUtils.getPathFromUri(getApplication(), uri);
                    Logger.d(TAG, "path3:"+path);
                }
            }
        });

        //CameraOcrImageActivity
        mBtn1.setOnClickListener(v -> {
            File publicFolder = FileUtils.getExternalPublicFolder(Environment.DIRECTORY_PICTURES);
            File file = new File(publicFolder, "camera3/" + System.currentTimeMillis() + ".png");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            Intent intent = new Intent(this, CameraOcrImageActivity.class);
            intent.putExtra(CameraOcrImageActivity.EXTRA_PATH, file.getAbsolutePath());
            intent.putExtra(CameraOcrImageActivity.EXTRA_TYPE, OcrType.BUSINESS_LICENSE);
//            startActivity(intent);
            mLauncher1.launch(intent);
        });


        //CaptureImageActivity
        mBtn2.setOnClickListener(v -> {
            File publicFolder = FileUtils.getExternalPublicFolder(Environment.DIRECTORY_PICTURES);
            File file = new File(publicFolder, "camera3/" + System.currentTimeMillis() + ".png");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            Intent intent = new Intent(this, CaptureImageActivity.class);
            intent.putExtra(CaptureImageActivity.EXTRA_PATH, file.getAbsolutePath());
//            startActivity(intent);
            mLauncher2.launch(intent);

        });
        //CaptureVideoActivity
        mBtn3.setOnClickListener(v -> {
            File publicFolder = FileUtils.getExternalPublicFolder(Environment.DIRECTORY_PICTURES);
            File file = new File(publicFolder, "camera3/" + System.currentTimeMillis() + ".mp4");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            Intent intent = new Intent(this, CaptureVideoActivity.class);
            intent.putExtra(CaptureVideoActivity.EXTRA_PATH, file.getAbsolutePath());
//            startActivity(intent);
            mLauncher3.launch(intent);

        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLauncher1.unregister();
        mLauncher2.unregister();
        mLauncher3.unregister();
    }
}